import asyncio
import pygame

import cfg
from bricks.mainBricks import MainBricks
from collapse.mainCollapse import MainCollapse
from collect_gold.mainCollectGold import GoldCollector
from debug import debug
from dragon.mainDragon import MainDragon
from gophers.mainGophers import MainGophers
from menu import drawMenus
from plane.mainPlane import MainPlane
from snakes.mainSnakes import MainSnaes
from tetris.mainTetris import MainTetris
from tower.mainTower import MainTower

bg = pygame.image.load('assets/image/bg.png')

class Game:
    def __init__(self, dims, FPS = 60):
        self.dims = dims
        self.FPS = FPS
        # 初始化pygame，预定义各种常量
        pygame.init()

    def playBgMusic(self):
        pygame.mixer.music.load('assets/music/bg1.mp3')
        pygame.mixer.music.play(-1)

    def blitBG(self, screen):
        # 缩放图片
        bgImg = pygame.transform.scale(pygame.transform.rotate(bg, 270), (self.dims[0], self.dims[1]))
        image_width, image_height = bgImg.get_size()
        screen_width, screen_height = self.dims
        image_x = (screen_width - image_width) // 2
        image_y = (screen_height - image_height) // 2
        screen.blit(bgImg, (image_x, image_y))

    # 游戏主循环所在函数需要由 async 定义
    async def start(self):
        # 初始化游戏界面（screen）：尺寸、背景色等
        screen = pygame.display.set_mode(self.dims)
        # 显示游戏名称
        pygame.display.set_caption("python游戏入门")
        # 初始化游戏时钟（clock），由于控制游戏帧率
        clock = pygame.time.Clock()
        # 播放背景音乐
        self.playBgMusic()

        # 游戏主循环
        while True:
            # 按照给定的 FPS 刷新游戏
            # clock.tick() 函数返回上一次调用该函数后经历的时间，单位为毫秒 ms
            # dt 记录上一帧接受之后经历的时间，单位为秒 m
            # 使用 dt 控制物体运动可以使游戏物理过程与帧率无关
            dt = clock.tick(self.FPS) / 1000.0
            # 使用 asyncio 同步
            # 此外游戏主体代码中不需要再考虑 asyncio
            await asyncio.sleep(0)

            # 游戏事件处理
            # 包括键盘、鼠标输入等
            for event in pygame.event.get():
                # 点击关闭窗口按钮或关闭网页
                if event.type == pygame.QUIT:
                    pygame.quit()

            # 以背景覆盖刷新游戏界面
            self.blitBG(screen)

            # 调用 debug 函数在游戏界面左上角显示游戏帧率
            debug(f"{clock.get_fps():.1f}", color='green')
            # 显示游戏菜单
            drawMenus(screen)
            # 进入所选游戏
            if cfg.inGame and cfg.concurrentGame == "聚宝盆":
                cGame = GoldCollector(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "贪吃蛇":
                cGame = MainSnaes(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "打飞机":
                cGame = MainPlane(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "打地鼠":
                cGame = MainGophers(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "跳跳龙":
                cGame = MainDragon(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "打砖块":
                cGame = MainBricks(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "消消乐":
                cGame = MainCollapse(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "俄罗斯方块":
                cGame = MainTetris(self.dims, FPS = 60)
                await cGame.start()
            if cfg.inGame and cfg.concurrentGame == "魔塔50层":
                cGame = MainTower(self.dims, FPS = 60)
                await cGame.start()

            # 将游戏界面内容输出至屏幕
            pygame.display.update()

        # 跳出游戏主循环，退出游戏
        pygame.quit()